//
//  TYSegmentIndicatorView.m
//  TYUIKit
//
//  Created by TuyaInc on 2019/2/28.
//

#import "TYSegmentIndicatorView.h"
#import "TYSegmentAnimator.h"

@implementation TYIndicatorModel



@end

@implementation TYSegmentIndicatorView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _indicatorLineViewHeight = 3;
        _veticalOffset = 0;
        _scrollEnabled = YES;
        _segmentPosition = TYSegmentIndicatorPositionBottom;
        _scrollAnimationDuration = 0.25;
        _indicatorLineWidth = TYSegmentLengthDynamic;
        _indicatorLineViewColor = [UIColor redColor];
        _indicatorLineViewCornerRadius = TYSegmentLengthDynamic;
        _animation = TYSegmentIndicatorAnimationExtend;
    }
    return self;
}

#pragma mark - JXCategoryIndicatorProtocol

- (void)refreshState:(TYIndicatorModel *)model {
    self.backgroundColor = self.indicatorLineViewColor;
    self.layer.cornerRadius = [self getIndicatorLineViewCornerRadius];
    
    CGFloat selectedLineWidth = [self getIndicatorLineViewWidth:model.selectedCellFrame];
    CGFloat x = model.selectedCellFrame.origin.x + (model.selectedCellFrame.size.width - selectedLineWidth)/2;
    CGFloat y = model.selectedCellFrame.size.height - self.indicatorLineViewHeight + self.veticalOffset;
    self.frame = CGRectMake(x, y, selectedLineWidth, self.indicatorLineViewHeight);
    
    
    TYSegmentIndicatorStyle indicatorStyle = model.indicatorStyle;
    if (indicatorStyle == TYSegmentIndicatorStyleNone || indicatorStyle == TYSegmentIndicatorStyleLine) {
        return;
    }
    // 创建一个CAShapeLayer
    CAShapeLayer *triangleLayer = [CAShapeLayer layer];
     
    // 设置三角形的位置
    triangleLayer.position = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
     
    // 设置三角形的边界
    triangleLayer.bounds = self.bounds;
     
    // 创建一个UIBezierPath来定义三角形的形状
    UIBezierPath *trianglePath = [UIBezierPath bezierPath];
    if (indicatorStyle == TYSegmentIndicatorStyleArrowUp) {
        [trianglePath moveToPoint:CGPointMake(0, self.frame.size.height)];
        [trianglePath addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height)];
        [trianglePath addLineToPoint:CGPointMake(self.frame.size.width / 2, 0)];
    }
    else if (indicatorStyle == TYSegmentIndicatorStyleArrowDown){
        [trianglePath moveToPoint:CGPointMake(0, 0)];
        [trianglePath addLineToPoint:CGPointMake(self.frame.size.width, 0)];
        [trianglePath addLineToPoint:CGPointMake(self.frame.size.width / 2, self.frame.size.height)];
    }
    
    [trianglePath closePath];
     
    // 将UIBezierPath设置为CAShapeLayer的路径
    triangleLayer.path = trianglePath.CGPath;
     
    // 设置三角形的填充颜色
    triangleLayer.fillColor = self.indicatorLineViewColor.CGColor;
     
    // 设置三角形的描边颜色
    triangleLayer.strokeColor = self.indicatorLineViewColor.CGColor;
    
    // 设置三角形的描边宽度
    triangleLayer.lineWidth = 0.01;
    self.backgroundColor = [UIColor clearColor];
    // 将CAShapeLayer添加到视图的层级结构中
    [self.layer addSublayer:triangleLayer];
}

- (void)contentScrollViewDidScroll:(TYIndicatorModel *)model {
    CGRect rightCellFrame = model.rightCellFrame;
    CGRect leftCellFrame = model.leftCellFrame;
    CGFloat percent = model.percent;
    CGFloat targetX = leftCellFrame.origin.x;
    CGFloat targetWidth = [self getIndicatorLineViewWidth:leftCellFrame];
    
    if (percent == 0) {
        targetX = leftCellFrame.origin.x + (leftCellFrame.size.width - targetWidth)/2.0;
    }else {
        CGFloat leftWidth = targetWidth;
        CGFloat rightWidth = [self getIndicatorLineViewWidth:rightCellFrame];
        
        CGFloat leftX = leftCellFrame.origin.x + (leftCellFrame.size.width - leftWidth)/2;
        CGFloat rightX = rightCellFrame.origin.x + (rightCellFrame.size.width - rightWidth)/2;
        
        if (self.animation == TYSegmentIndicatorAnimationTranslate) {
            targetX = [TYSegmentAnimator interpolationFrom:leftX to:rightX percent:percent];
            
            if (self.indicatorLineWidth == TYSegmentLengthDynamic) {
                targetWidth = [TYSegmentAnimator interpolationFrom:leftCellFrame.size.width to:rightCellFrame.size.width percent:percent];
            }
        } else if (self.animation == TYSegmentIndicatorAnimationExtend) {
            CGFloat offsetX = 10;
            CGFloat maxWidth = rightX - leftX + rightWidth - offsetX*2;
            if (percent <= 0.5) {
                targetX = [TYSegmentAnimator interpolationFrom:leftX to:leftX + offsetX percent:percent*2];;
                targetWidth = [TYSegmentAnimator interpolationFrom:leftWidth to:maxWidth percent:percent*2];
            }else {
                targetX = [TYSegmentAnimator interpolationFrom:(leftX + offsetX) to:rightX percent:(percent - 0.5)*2];
                targetWidth = [TYSegmentAnimator interpolationFrom:maxWidth to:rightWidth percent:(percent - 0.5)*2];
            }
        }
    }
    
    //允许变动frame的情况：1、允许滚动；2、不允许滚动，但是已经通过手势滚动切换一页内容了；
    if (self.scrollEnabled == YES || (self.scrollEnabled == NO && percent == 0)) {
        CGRect frame = self.frame;
        frame.origin.x = targetX;
        frame.size.width = targetWidth;
        self.frame = frame;
    }
}

- (void)selectedCell:(TYIndicatorModel *)model {
    CGFloat targetWidth = [self getIndicatorLineViewWidth:model.selectedCellFrame];
    CGRect toFrame = self.frame;
    toFrame.origin.x = model.selectedCellFrame.origin.x + (model.selectedCellFrame.size.width - targetWidth)/2.0;
    toFrame.size.width = targetWidth;
    
    if (self.scrollEnabled) {
        [UIView animateWithDuration:self.scrollAnimationDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.frame = toFrame;
        } completion:^(BOOL finished) {
            
        }];
    }else {
        self.frame = toFrame;
    }
}

# pragma mark - Private

- (CGFloat)getIndicatorLineViewCornerRadius
{
    if (self.indicatorLineViewCornerRadius == TYSegmentLengthDynamic) {
        return self.indicatorLineViewHeight/2;
    }
    return self.indicatorLineViewCornerRadius;
}

- (CGFloat)getIndicatorLineViewWidth:(CGRect)cellFrame
{
    if (self.indicatorLineWidth == TYSegmentLengthDynamic) {
        return cellFrame.size.width;
    }
    return self.indicatorLineWidth;
}

@end
